package com.example.elderlycare.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.elderlycare.dao.entity.Relative;
import com.example.elderlycare.dao.mapper.RelativeMapper;
import com.example.elderlycare.service.RelativeService;
import com.example.elderlycare.service.entity.RelativeItem;
import com.example.elderlycare.service.entity.RelativeListResponseBody;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class RelativeServiceImpl extends ServiceImpl<RelativeMapper, Relative> implements
        RelativeService {

    @Resource
    RelativeMapper relativeMapper;

    @Override
    public RelativeListResponseBody getRelativesByElderId(Integer elderId) {
        QueryWrapper<Relative> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("elder_id", elderId);
        Long total = relativeMapper.selectCount(queryWrapper);
        List<RelativeItem> relativeList = new ArrayList<>();
        List<Relative> relatives = relativeMapper.getRelativeListByElderId(elderId);
        for (Relative relative : relatives) {
            RelativeItem relativeItem = RelativeItem.builder()
                    .id(relative.getId())
                    .name(relative.getName())
                    .number(relative.getNumber())
                    .phone(relative.getPhone())
                    .relationId(relative.getRelationId())
                    .relation(relative.getRelation())
                    .certificateId(relative.getCertificateId())
                    .certificate(relative.getCertificate())
                    .priority(relative.getPriority())
                    .build();
            relativeList.add(relativeItem);
        }
        return RelativeListResponseBody.builder().relativeList(relativeList)
                .total(Math.toIntExact(total))
                .build();
    }

}
